package com.yxkj.syh.app.huarong.activities.orders.pay_list;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.BaseFragment;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.PayOrderListAdp;
import com.yxkj.syh.app.huarong.bean.PagesRequest;
import com.yxkj.syh.app.huarong.bean.PayOrderInfo;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.FragmentPayOrdersBinding;

import java.util.ArrayList;
import java.util.List;

/**
 *  支出单 支出单列表
 */
public class PayOrdersFragment extends BaseFragment<FragmentPayOrdersBinding,PayOrdersVM> {

    private PagesRequest ordersRequest;
    private PayOrderListAdp mPayOrderListAdp;

    public PayOrdersFragment() {
    }

    public static PayOrdersFragment newInstance() {
        PayOrdersFragment fragment = new PayOrdersFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.payOrdersVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_pay_orders;
    }

    @Override
    protected void initView() {
        mPayOrderListAdp = new PayOrderListAdp();
        mPayOrderListAdp.setIvIconRec(R.mipmap.ic_no_pay_order);
        mVDBinding.rvPayOrders.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mPayOrderListAdp.bindToRecycleView(mVDBinding.rvPayOrders);
        mPayOrderListAdp.setOnViewClick(new PayOrderListAdp.OnViewClick() {
            @Override
            public void onItem(PayOrderInfo payOrderInfo) {
                ARouter.getInstance().build(ArouterPath.PAY_ORDER_DETAIL_ACTIVITY).withLong("id",payOrderInfo.getId()).navigation();
            }

            @Override
            public void onConfirmReceive(PayOrderInfo payOrderInfo) {
                mViewModel.payOrderReceiveMoney(payOrderInfo.getId()+"");
            }
        });

        mVDBinding.srlPayOrders.setOnRefreshListener(refreshLayout -> {
            mPayOrderListAdp.clear();
            initData();
        });

        mVDBinding.srlPayOrders.setOnLoadMoreListener(refreshLayout -> {
            ordersRequest.setPageIndex(ordersRequest.getPageIndex().intValue() + 1);
            mViewModel.payOrderList(ordersRequest);
        });
        mVDBinding.srlPayOrders.setEnableLoadMore(false);
    }

    @Override
    protected void initData() {

        ordersRequest = new PagesRequest();
        ordersRequest.setPageIndex(1);
        ordersRequest.setPageSize(10);
        ordersRequest.setSortFields("id_d");

        List<PagesRequest.SqlParam> sqlParams = new ArrayList<>();
        ordersRequest.setQueryList(sqlParams);

        mViewModel.payOrderList(ordersRequest);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldPayOrderListResponse.observe(this, orderInfos -> {
            if (mVDBinding.srlPayOrders.isRefreshing()) {
                mVDBinding.srlPayOrders.finishRefresh();
            } else if (mVDBinding.srlPayOrders.isLoading()) {
                mVDBinding.srlPayOrders.finishLoadMore();
            }
            if (orderInfos != null) {
                mPayOrderListAdp.addAll(orderInfos.getRecords());
                if (ordersRequest.getPageIndex() < orderInfos.getPages() - 1) {
                    mVDBinding.srlPayOrders.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlPayOrders.setEnableLoadMore(false);
                }
            }
        });

        mViewModel.mldReceiveMoney.observe(this, aBoolean -> {
            if (aBoolean){
                mPayOrderListAdp.clear();
                initData();
            }
        });
    }

    @Override
    protected void visibleAgain() {
        mPayOrderListAdp.clear();
        initData();
    }
}
